<template>
  <el-form-item label="标签">
    <el-input v-model="currentItem.label" />
  </el-form-item>
  <el-form-item label="宽度">
    <el-input-number v-model="componentStyle.width" :min="1" :max="500" />
  </el-form-item>
  <el-table :data="componentData.options" style="width: 100%" max-height="500">
    <el-table-column fixed prop="label" label="label">
      <template #default="scope">
        <el-input v-model="scope.row.label" />
      </template>
    </el-table-column>
    <el-table-column prop="value" label="value">
      <template #default="scope">
        <el-input v-model="scope.row.value" />
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="Operations">
      <template #default="scope">
        <el-button
          link
          type="primary"
          size="small"
          @click.prevent="deleteRow(scope.$index)"
        >
          移除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-button class="mt-4" style="width: 100%" @click="onAddItem">
    新增
  </el-button>
</template>

<script setup lang="ts">
import { computed, PropType } from "vue";
import { DraggableTreeNode } from "../../../type";
import { RadioComponentProps, RadioComponentData } from "./display-radio";

const props = defineProps({
  currentItem: {
    type: Object as PropType<
      DraggableTreeNode<RadioComponentProps, RadioComponentData>
    >,
    default: () => {
      return {};
    },
  },
});
const componentStyle = computed({
  get() {
    return props.currentItem?.component?.componentStyle || {};
  },
  set(value) {
    if (props.currentItem?.component) {
      props.currentItem.component.componentStyle = value;
    }
  },
});
// const componentProps = computed({
//   get() {
//     return props.currentItem?.component?.componentProps || {};
//   },
//   set(value) {
//     if (props.currentItem?.component) {
//       props.currentItem.component.componentProps = value;
//     }
//   },
// });
const componentData = computed({
  get() {
    return (
      props.currentItem?.component?.componentData || {
        options: [],
      }
    );
  },
  set(value) {
    if (props.currentItem?.component) {
      props.currentItem.component.componentData = value;
    }
  },
});
const deleteRow = (index: number) => {
  componentData.value.options.splice(index, 1);
};

const onAddItem = () => {
  componentData.value.options.push({
    label: `label${componentData.value.options.length}`,
    value: `value${componentData.value.options.length}`,
  });
};
const emit = defineEmits(["update:currentItem"]);
const currentItem = computed({
  get() {
    return (
      props.currentItem || {
        label: "",
        component: {},
      }
    );
  },
  set(value) {
    emit("update:currentItem", value);
  },
});
</script>

<style lang="scss" scoped></style>
